/*
 * foxmob.cn has all rights reserved.
 */
package foxmob.mooc.user;
import static spark.Spark.*;


import foxmob.mooc.DbAccess;
import foxmob.mooc.Utils;

import foxmob.mooc.persist.tables.User;
import foxmob.mooc.persist.tables.TrainingMember;
import spark.Request;
import spark.Response;


import org.jooq.*;
//import me.chanjar.weixin.mp.api.

import java.util.List;
//import me.chanjar.weixin.mp.api.
//


/**
 *
 * @author liyusen
 */

public class TrainingReg {

    public static void  init(){
        post("/tr/reg",TrainingReg::register);
        // get info

        get("/tr/reg",(req,res)->{
            req.session().attribute("name","");
            res.redirect("/",200);
            return "lg";
        });


    }

    public static String register(Request req, Response res){
        if(req.session().attribute("userid")==null ||
                req.session().attribute("userid").toString().length()==0){
            return "{\"id\":0}";
        }

        String[] values = Utils.getMultipart(new String[]{"pupilno","name","appsheet"},req);

        if(values.length!=3) return "0";
        int n =0;


        try{

            DSLContext dsl = DbAccess.getDsl();

            Result<?> result = dsl.insertInto(TrainingMember.TRAINING_MEMBER,
                    TrainingMember.TRAINING_MEMBER.PUPILNO,
                    TrainingMember.TRAINING_MEMBER.NAME,
                    TrainingMember.TRAINING_MEMBER.APPSHEET,
                    TrainingMember.TRAINING_MEMBER.USERID).
                    // 从session里拿已经登录了的userid
                    values(values[0],values[1],values[2],req.session().attribute("userid")).
                    returning(TrainingMember.TRAINING_MEMBER.ID).fetch();
            for(Record r : result ){
                n = r.getValue(TrainingMember.TRAINING_MEMBER.ID);
                break;

            }


        }catch(Exception e){
            e.printStackTrace();


        }


        return String.format("{\"id\":%d}",n);
    }

    public static String getUsers(Request req, Response res){
        String rs = "[";
        int start = Integer.parseInt(req.queryParams("start"));
        int pgsize = Integer.parseInt(req.queryParams("pgsize"));
        String pattern = String.format("%s%s%s","%",req.queryParams("pattern"),"%");
        System.out.println(pattern);
        try{
            DSLContext dsl = DbAccess.getDsl();
            //Book Book.BOOK = new Book();
            Result<Record> result = dsl.select()
                    .from(User.USER).where(User.USER.USERNAME.like(pattern))
                    .or(User.USER.NAME.like(pattern))
                    .limit(pgsize).offset(start).fetch();

            for (Record r : result) {
                int userid = r.getValue(User.USER.ID);
                String name = r.getValue(User.USER.NAME);;
                String username = r.getValue(User.USER.USERNAME);
                String email = r.getValue(User.USER.EMAIL);
                String s = String.format("[%d,\"%s\",\"%s\",\"%s\"],",
                        userid,name,username,email);

                rs = String.format("%s%s",rs,s);
            }
            rs = String.format("%s[]]",rs);



        }catch(Exception e){
            e.printStackTrace();
            rs = "[[]]";

        }


        return rs;
    }

}
